﻿using OnlineAuction.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineAuction.Admin
{
    public partial class ManageHome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["UserInfo"] == null
                || Request.Cookies["UserInfo"]["Role"].ToString().CompareTo("Admin") != 0)
            {
                Response.Redirect("../Home.aspx");
            }
            else if (!Page.IsPostBack)
            {
                //ddlDay.Items.Add("");
                //ddlMonth.Items.Add("");
                //for (int i = 1; i <= 31; i++)
                //{
                //    if (i <= 12)
                //    {
                //        ddlMonth.Items.Add(i.ToString());
                //    }
                //    ddlDay.Items.Add(i.ToString());
                //}
                AuctionBO bo = new AuctionBO();
                ddlYear.DataSource = bo.GetYear();
                ddlYear.DataBind();
                ddlYear_SelectedIndexChanged(null, null);
            }
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDay.Items.Clear();
            ddlMonth.Items.Clear();
            AuctionBO bo = new AuctionBO();
            ddlMonth.DataSource = bo.GetMonth(ddlYear.SelectedValue);
            ddlMonth.DataBind();
            ddlMonth.SelectedIndex = 0;
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDay.Items.Clear();
            AuctionBO bo = new AuctionBO();
            ddlDay.DataSource = bo.GetDay(ddlYear.SelectedValue, ddlMonth.SelectedValue);
            ddlDay.DataBind();
        }

        protected void UpdateTimer_Tick(object sender, EventArgs e)
        {
            AuctionBO bo = new AuctionBO();
            grvRevenue.DataSource = bo.Revenue(ddlYear.SelectedValue,
                                               ddlMonth.SelectedValue,
                                               ddlDay.SelectedValue);
            grvRevenue.DataBind();
        }
        // Declare variable used to store value of Total
        private double TotalSales = 0f;
        protected void grvRevenue_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // check row type
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // if row type is DataRow, add ProductSales value to TotalSales
                TotalSales += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "FeeMoney"));
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                // If row type is footer, show calculated total value
                e.Row.Cells[7].Text = TotalSales.ToString();
            }
        }
    }
}